package cn.buddha.jesj.thread;

import cn.buddha.jesj.thread.jvm.MyCallable;
import cn.buddha.jesj.thread.jvm.MyRunnable;
import cn.buddha.jesj.thread.jvm.MyThread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassName ThreadTest
 * @Description 线程测试
 * @Author shihao
 * @Date 2021/6/20 0020 13:14
 * @Version 1.0
 **/
public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {
//        threadTest();

//        runnableTest();

        callableTest();
    }

    private static void threadTest() {
        MyThread.builder().title("thread-线程A").build().start();
        MyThread.builder().title("thread-线程B").build().start();
        MyThread.builder().title("thread-线程C").build().start();
        MyThread.builder().title("thread-线程D").build().start();
    }

    public static void runnableTest() {
        new Thread(MyRunnable.builder().title("runnable-线程A").build()).start();
        new Thread(MyRunnable.builder().title("runnable-线程B").build()).start();
        new Thread(MyRunnable.builder().title("runnable-线程C").build()).start();
        new Thread(MyRunnable.builder().title("runnable-线程D").build()).start();
    }

    private static void callableTest() {
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
        try {
            System.err.println(futureTask.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

}
